package com.ctim.web.user;

import com.ctim.common.exception.EnumPcsService;
import com.ctim.common.pcs.PcsListResult;
import com.ctim.common.pcs.PcsResult;
import com.ctim.model.user.User;
import com.ctim.service.bean.RoleBean;
import com.ctim.service.user.RoleService;
import com.ctim.web.common.BaseController;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * User: lb on 2016/9/2.
 * Date:2016-09-02-15:23
 * desc：
 */
@RestController
@RequestMapping("/rest/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public PcsListResult roleList(@RequestParam(value="name",required = false) String name, @RequestParam(value="pageIndex",required = false) Integer pageIndex,
                                  @RequestParam("pageSize") Integer pageSize) {
        return roleService.selectRoleList(name, getCurPage(pageIndex), getPageSize(pageSize));
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public PcsResult addRole(RoleBean roleBean) {
        Integer userId = getUserId();
        roleBean.setCreateUid(userId);
        roleService.addRole(roleBean);
        return newResult();
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public PcsResult deleteRole(@RequestParam("roleId") Integer roleId) {
        Assert.notNull(roleId,"角色ID不能为空");
        Integer userId = getUserId();
        boolean ret = roleService.deleteRole(roleId,userId);
        return ret?newResult():failedResult(EnumPcsService.ERROR_OPERATE);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public PcsResult updateRole( RoleBean roleBean) {
        Assert.notNull(roleBean.getId(),"角色ID不能为空");
        Integer userId = getUserId();
        roleBean.setUpdateUid(userId);
        roleService.updateRole(roleBean);
        return newResult();
    }


}
